package com.yhy.server.service;

import com.alibaba.fastjson.JSON;
import com.yhy.domain.mongo.Video;
import com.yhy.dubbo.api.mongo.VideoApi;
import com.yhy.server.interceptor.UserHolder;

import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class VideoMQService {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Reference
    private VideoApi videoApi;

    /**
     * 发送视频的mq消息
     * 传递参数:1.vid 2.userId 3.type
     *
     * 请求参数:1.publishId(动态id)
     *          2.type(操作类型)
     *
     *
     */
    public void sendMsg(String publishId,Integer type){
        try{
            Long userId = UserHolder.getUserId();

            Map<String, String> message = new HashMap();
            Video video =videoApi.findById(publishId);
            message.put("vid",video.getVid().toString());
            message.put("type",type.toString());
            message.put("userId",userId.toString());
            rocketMQTemplate.convertAndSend("tanhua-videoR", JSON.toJSONString(message));

        }catch (Exception e){
            e.printStackTrace();
            System.out.println("视频MQ发送失败,publishId:"+publishId+"==type:"+type);
        }
    }
}
